<!DOCTYPE html>
<html>
<head>
    <title>小工具</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./webjars/bootstrap/4.5.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/common.css">
    <script src="./webjars/jquery/3.4.1/jquery.min.js"></script>
    <script src="./webjars/popper.js/1.16.0/popper.min.js"></script>
    <script src="./webjars/bootstrap/4.5.3/js/bootstrap.min.js"></script>
    <script src="./webjars/layer/dist/layer.js"></script>
    <link rel="icon" href="./img/favicon.ico" type="image/x-icon"/>
    <link rel="shortcut icon" href="./img/favicon.ico" type="image/x-icon"/>
    <style>
        .full-h {
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .btn-width {
            width: 100px;
        }
    </style>
</head>
<body>
<div id="head">
</div>
<script>
    $("#head").load("./head.html", function () {
        $("#ymlAndProp").addClass("active");
    });
</script>
<form class="container-fluid" style="margin-top: 5rem">
    <div class="row  mt-9 pt-3" style="min-height: 640px ; ">
        <div class="col full-h">
            <div class="form-group full-h">
                <textarea class="form-control full-h" rows="4" id="props" placeholder="properties"></textarea>
            </div>
        </div>
        <div class="col-1"
             style="display: flex;flex-direction: column;justify-content: center;    align-items: center;">
            <div class="btn-group" style="display: flex;justify-content: flex-start;">
                <button type="button" class="btn btn-custom btn-width" id="toYml">toYml--></button>
            </div>
            <div class="btn-group mt-4" style="display: flex;justify-content: flex-start;">
                <button type="button" class="btn btn-custom-end btn-width" id="toProp"><--toProp</button>
            </div>
        </div>
        <div class="col full-h">
            <div class="form-group full-h">
                <textarea class="form-control full-h" rows="4" id="yml" placeholder="yml"></textarea>
            </div>
        </div>
    </div>
</form>
</body>
<script>
    $(document).ready(function () {
        $("#toYml").click(function () {
            var content = $("#props").val();
            if (!content) {
                return;
            }
            var layerIndex = layer.load(2);
            convert(1, content, function (data) {
                layer.close(layerIndex);
                $("#yml").val(data);
            });
        });
        $("#toProp").click(function () {
            var content = $("#yml").val();
            if (!content) {
                return;
            }
            var layerIndex = layer.load(2);
            convert(2, content, function (data) {
                layer.close(layerIndex);
                $("#props").val(data);
            });
        })
    });

    function convert(type, content, finish) {
        $.ajax({
            url:"/yml",
            type:"POST",
            contentType:"application/json; charset=utf-8",
            data:JSON.stringify({
                type: type,
                content: content
            }),
            success: function (data, status) {
                if (status === 'success') {
                    finish(data.data)
                } else {
                    finish(error());
                }
            }
        })

    }
</script>
</html>
